import java.util.*;

class Node {
    private int data = 0;
    private Node next = null;

    public Node() {}

    public Node(int data) {
        this.data = data;
    }

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node set(Node next) {
        this.next = next;
        return this;
    }

    public Node set(int data) {
        this.data = data;
        return this;
    }

    public Node getNext() {
        return this.next;
    }

    public int get() {
        return this.data;
    }
}

public class MyListTest {
    static final Scanner sc = new Scanner(System.in);
    //有头 / 无头
    //单向 / 双向
    //循环 / 不循环
    //有序 / 无序

    //有头单向不循环链表
    static public void main(String [] args) {
        Node head = new Node(), newObj = null;
        Node tail = head;
        int num;

        while (true) {
            System.out.printf("请输入数据: ");
            num = sc.nextInt();

            if (num == -1)
                break;

            newObj = new Node(num);

            for (tail = head;
                    tail.getNext() != null && num > tail.getNext().get();
                    tail = tail.getNext())
                ;
            newObj.set(tail.getNext());
            tail.set(newObj);
            // tail = newObj;
        }

        tail = head.getNext();
        while (tail != null) {
            System.out.printf("%d ", tail.get());
            tail = tail.getNext();
        }
        System.out.println();
    }

    //无头单向不循环链表
    static public void noHeadList() {
        Node head = null, newObj = null, tail = null;
        int num;

        while (true) {
            System.out.printf("请输入数据: ");
            num = sc.nextInt();

            if (num == -1)
                break;

            newObj = new Node(num);

            if (head == null) {
                head = newObj;
            } else {
                tail.set(newObj);
            }

            tail = newObj;
        }

        /*
         * head.get()
         * head.getNext().get()
         * head.getNext().getNext().get()
         * head.getNext().getNext().getNext().get()
         */

        tail = head;
        while (tail != null) {
            System.out.printf("%d ", tail.get());
            tail = tail.getNext();
        }
        System.out.println();
    }
}
